package com.cooge.sharevideo.web.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.cooge.sharevideo.base.BaseDao;
import com.cooge.sharevideo.web.dao.VideoDao;
import com.cooge.sharevideo.web.pojo.Video;
@Repository
public class VideoDaoImpl extends BaseDao<Video> implements VideoDao {

	@Override
	public Class<Video> getEntityClass() {
		return Video.class;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Video> findVideolistByMenu(Long menuId,Integer first,Integer max) {
		
		String sql = "select {v.*} from video v left join episode_video ev on ev.video_Id  = v.id left join menu_episode me on me.episode_Id = ev.episode_id where me.menu_Id = ?";
		
		SQLQuery sqlQuery = this.getSession().createSQLQuery(sql);
		
		sqlQuery.addEntity("v", Video.class);
		sqlQuery.setFirstResult(first);
		sqlQuery.setMaxResults(max);
		sqlQuery.setLong(0, menuId);
		
		return sqlQuery.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Video> getPageList(Video t, int first, int max, String order,String asc) throws Exception {
		Criteria cri =super.getCriteria(t, first, max, order, asc);
		cri.add(Restrictions.or(Property.forName("title").like("%" + t.getTitle() + "%"),Property.forName("introduction").like("%" + t.getIntroduction() + "%")));
		return cri.list();
	}
}
